#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution
{
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
    {
        vector<int> ret;
        int n1 = 0, n2 = 0;
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        while (n1 < nums1.size() && n2 < nums2.size())
        {
            if (nums1[n1] == nums2[n2])
            {
                ret.push_back(nums1[n1]);
                ++n1;
                ++n2;
            }
            else
            {
                if (nums1[n1] < nums2[n2])
                {
                    ++n1;
                }
                else
                {
                    ++n2;
                }
            }
            if (n1&&n1<nums1.size())
            {
                while (nums1[n1] == nums1[n1 - 1])
                {
                    ++n1;
                }
            }
            if (n2 && n2 < nums2.size())
            {
                while (nums2[n2] == nums2[n2 - 1])
                {
                    ++n2;
                }
            }
            
        }
        return ret;
    }
};

int main()
{
    vector<int> nums1{4,2,1,3,5 ,34,5,3,13,53,434,61,346,7,547};
    vector<int> nums2{ 2,1,6,0,3,4,5,1,6,14,634,6454,7,1 };
    vector<int> ret;
    Solution s;
    ret = s.intersection(nums1, nums2);
    for (auto e : ret)
    {
        cout << e << " ";
    }
    
    return 0;
}